package xj.graph2d.app.prop;

import static xj.app.BaseApplicationFrame.getResourceString;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import xj.graph2d.Document;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.DrawApp;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;

public class DocumentPropertyListener 
  extends DrawAction 
  implements ActionListener {

  class DocumentOptionPanel extends DialogPanel {

    public DocumentOptionPanel(String name) {
      super(name);
      JLabel nameLabel = new JLabel(getResourceString("ProjectItemName.text") + " ");
      nameField = new JTextField();
      nameField.setEnabled(false);
      JLabel typeLabel = new JLabel(getResourceString("ProjectItemType.text") + " ");
      typeField = new JComboBox();
      typeField.addItem("UML Class Diagram");
      typeField.addItem("UML Package Diagram");
      typeField.addItem("UML Use Case Diagram");
      typeField.addItem("UML State Diagram");
      typeField.addItem("UML Sequence Diagram");
      typeField.addItem("UML Activity Diagram");
      typeField.setEnabled(false);

      JLabel authorLabel = new JLabel(getResourceString("Author.text") + " ");
      authorField = new JTextField();
      JLabel titleLabel = new JLabel(getResourceString("Title.text") + " ");
      titleField = new JTextField();
      JLabel orgLabel = new JLabel(getResourceString("Organization.text") + " ");
      orgField = new JTextField();

      JLabel descriptionLabel = new JLabel(getResourceString("Description.text") + " ");
      descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
      descriptionField = new JTextArea(4, 40);
      descriptionField.setBorder(BorderFactory.createEtchedBorder());

      JLabel ctimeLabel1 = new JLabel(getResourceString("Create.text") + " ");
      ctimeLabel = new JLabel();
      JLabel mtimeLabel1 = new JLabel(getResourceString("LastModified.text") + " ");
      mtimeLabel = new JLabel();

      addRow(nameLabel, nameField);
      addRow(typeLabel, typeField);
      addRow(titleLabel, titleField);
      addRow(authorLabel, authorField);
      addRow(orgLabel, orgField);
      addRow(descriptionLabel, new JScrollPane(descriptionField));
      addRow(ctimeLabel1, ctimeLabel);
      addRow(mtimeLabel1, mtimeLabel);

      setPreferredSize(new Dimension(400, 330));
      setOption();
    }

    @Override
    public void setOption() {
      SimpleDrawDocument doc = getDocument();
      if (doc != null) {
	String name = doc.getName();
	Document.DocumentType type = doc.getDocumentType();
	String typeStr = type.toString();
	typeStr = typeStr.replaceAll("_", " ");
	String title = doc.getTitle();
	String author = doc.getAuthor();
	String org = doc.getOrganization();
	String des = doc.getDescription();

	nameField.setText(name);
	typeField.setSelectedItem(typeStr);
	authorField.setText(author);
	titleField.setText(title);
	orgField.setText(org);
	descriptionField.setText(des);
	ctimeLabel.setText((new Date(doc.getCreationTime())).toString());
	mtimeLabel.setText((new Date(doc.getLastModificationTime())).toString());
      }
    }

    @Override
    public void applyOption() {
      SimpleDrawDocument doc = getDocument();
      if (doc != null) {
	String title = titleField.getText();
	String author = authorField.getText();
	String org = orgField.getText();
	String des = descriptionField.getText();
	doc.setAuthor(author);
	doc.setTitle(title);
	doc.setOrganization(org);
	doc.setDescription(des);
      }
    }

    public SimpleDrawDocument getDocument() { 
      return main.getDoc();
    }

    protected JTextField nameField;

    protected JComboBox typeField;

    // author
    // title
    // description
    protected JTextField authorField;

    protected JTextField titleField;

    protected JTextField orgField;

    protected JTextArea descriptionField;

    protected JLabel ctimeLabel;

    protected JLabel mtimeLabel;

  }

  protected TitledDialogPanel documentOptionDialog = null;
  protected DocumentOptionPanel documentPanel = null;

  public void actionPerformed(ActionEvent e) {
    if (documentOptionDialog == null) {
      //String documentPanelTitle = getResourceString("Document.title");
      String documentDialogTitle = getResourceString("DocumentDialog.title");
      String documentHeader = getResourceString("DocumentDialog.header");
      String[] documentText = new String[] { 
	getResourceString("DocumentDialog.text1"),
	getResourceString("DocumentDialog.text2"),	
      };
      Icon icon = DrawApp.getIcon("NewWizIcon");

      DialogPanel[] options = { 
	documentPanel = new DocumentOptionPanel(null), //documentPanelTitle), 
      };

      documentOptionDialog = new TitledDialogPanel(main.getJFrame(), 
						   JOptionPane.OK_CANCEL_OPTION, 
						   null, options);
      documentOptionDialog.setTitle(documentDialogTitle);
      documentOptionDialog.setTitlePaneText(documentHeader, documentText, icon);
    }
    documentOptionDialog.setOptions();
    documentOptionDialog.showOptionDialog();
  }

}
